<template>
  <div class="dashboard-container">
    <div class="app-container">

      <!-- 卡片组件 -->
      <el-card class="box-card">

        <!-- 导航栏组件 -->
        <el-tabs v-model="activeName">
          <el-tab-pane class="tab-pane" label="用户管理" name="first">

            <!-- Layout布局,行和列,分24份 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              class="department-header"
            >
              <!-- 左侧 -->
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>

              <!-- 右侧 -->
              <el-col :span="4">
                <!-- 右侧Layout布局 -->
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <!-- 树形结构 -->
            <el-tree
              class="departments-tree"
              :data="treeData"
              :props="defaultProps"
              :default-expand-all="true"
            >
              <!-- 结构复用 -->
              <template slot-scope="{ data }">
                <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="addDept(data.id)">添加子部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editDept(data.id)">编辑部门</el-dropdown-item>
                            <el-dropdown-item v-if="data && !data.children" @click.native="delDept(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>

          </el-tab-pane>
        </el-tabs>

        <!-- 弹框 -->
        <el-dialog
          :title="isEdit?'编辑部门':'添加部门'"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <deptDialog
            v-if="showDialog"
            :parent-id="parentId"
            :is-edit="isEdit"
            :origin-list="originList"
            @update-depart="updateDepart"
            @close="showDialog=false"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartmentsAPI, delDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import deptDialog from './deptDialog'
export default {
  name: 'Departments',
  components: {
    deptDialog
  },
  data() {
    return {
      activeName: 'first',
      // 树形控件数据
      treeData: [],
      // 定义结构显示
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 控制弹框显示
      showDialog: false,
      // 父级id
      parentId: '',
      // 是否是编辑
      isEdit: false,
      // 整理好的数据
      originList: []
    }
  },
  created() {
    // 在钩子函数里调用定义的方法
    this.getDepartments()
  },
  methods: {

    // 获取组织结构数据
    async getDepartments() {
      // 调用接口,获取数据
      const res = await getDepartmentsAPI()
      // 调用封装的方法处理数据,并赋值给treeData然后渲染
      this.treeData = tranListToTreeData(res.data.depts, '')

      // 整理数据传个子组件,做重复验证,用map方法
      this.originList = res.data.depts.map(item => ({
        id: item.id,
        code: item.code,
        pid: item.pid,
        name: item.name
      }))
    },

    // 点击添加
    addDept(id) {
      // 点击的是添加
      this.isEdit = false

      // 父传子接
      this.parentId = id

      // 控制弹出框
      this.showDialog = true
    },

    // 点击编辑
    editDept(id) {
      // 点击的是编辑
      this.isEdit = true

      // 父传子接
      this.parentId = id

      // 控制弹出框
      this.showDialog = true
    },

    // 点击删除
    async delDept(id) {
      // 提示框
      const delRes = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      console.log(delRes)

      // 提示框反馈的取消按钮,取消删除
      if (delRes === 'cancel') return this.$message('已取消')

      // 提示框反馈的确定按钮,确定删除
      const res = await delDepartments(id)
      // 删除成功反馈用户
      this.$message.success(res.message)
      // 重新获取渲染数据
      this.getDepartments()
    },

    // 父亲监听子派发的事件
    updateDepart() {
      // 先关闭弹出层
      this.showDialog = false
      // 重新获取组织结构
      this.getDepartments()
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
</style>
